import { observable, extendObservable, action, toJS } from "mobx"
import {
  getRelayModuleInfo,
  getLoad,
  getDayChart,
  getLoadChart,
  sealHead,
  electricity,
  alarm,
  battery,
  getFsuList,
  getPueChart,
  environment,
  updateFSU,
  getEnergyConservationData,
  rectifierModule,
  chart,
  relayModuleSet,
  getFsuInfo,
  getFsuControlInfo,
  alternatingCurrent,
  relayModuleControl,
  fsuControl,
} from "util/api"
import { message as Message } from "antd"
import store from "store"

//导出
export default class ScreenModal {
  constructor() {
    this.reset(true)
  }

  @action
  reset = (init, ruleOut = []) => {
    const state = {
      visible: {},
      currentType: null,
      loadData: {},
      electricity: {},
      deviceId: "",
      rectifierModule: {},
      battery: {},
      batteryIndex: 0,
      environment: {},
      ring: {},
      alternatingCurrent: {},
      alarm: {},
      fsuInfo: {},
      fsuControlInfo: {},
      relayModuleInfo: [],
      relayModuleRecording: [],
      fsuControRecording: {},
      chart: {},
      chartCount: 0,
      fsuList: [],
      pueChart: [],
      electricityLoadding: false,
      loadChartLoading: false,
      pueLoading: false,
      address: "",
      message: "",
      createTime: "",
      currentSuId: "",
      dayChart: [],
      energyConservationData: {},
      loadChart: {},
    }

    if (init) {
      extendObservable(this, state)
    } else {
      Object.keys(state).forEach((key) => {
        if (ruleOut.includes(key)) {
        } else {
          this[key] = state[key]
        }
      })
    }
  }

  @action
  onWillMount = async (location, match, history) => {
    this.reset()
  }
  @action
  cancle = async (type) => {
    let obj = toJS(this.visible)
    if (type) {
      obj[type] = false
    } else {
      let keys = _.keys(obj)
      _.forEach(keys, (item) => {
        obj[item] = false
      })
    }

    this.visible = obj
  }
  @action
  getLoad = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    let res = await getLoad(params)
    this.loadData = res.data || {}
  }
  @action
  getFsuList = async (params = {}) => {
    let res = await getFsuList(params)
    if (res.result === 0) {
      this.fsuList = res.data.list
      return res.data.list
    }
  }

  @action
  setAddress = async (suId) => {
    this.address = suId
  }
  @action
  setMessage = async (suId) => {
    this.message = suId
  }
  @action
  setCurrentSuId = async (suId) => {
    this.currentSuId = suId
    localStorage.setItem("currentSuId", suId)
  }
  @action
  fsuFirstLoad = async (params = {}) => {
    let data = await this.getFsuList().then()
    if (data[0]) {
      let suId = data[0].suID
      let row = localStorage.getItem("currentSuId")
      if (row) {
        suId = row
      } else {
        suId = data[0].suID
        localStorage.setItem("currentSuId", suId)
      }
      this.getFsuInfo({ suId })
      this.currentSuId = suId
    }
  }

  @action
  setBatteryIndex = async (index) => {
    this.batteryIndex = index
  }
  @action
  setChartCount = async (index) => {
    this.chartCount = index
  }
  @action
  updateFSU = async (params = {}) => {
    let res = await updateFSU({ ...params, suId: this.currentSuId })
    if (res.result === 0) {
      Message.success("修改成功!")
    }
  }
  @action
  getLoadChart = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    this.loadChartLoading = true
    let res = await getLoadChart(params)
    if (res.data) {
      this.loadChart = res.data || []
    } else {
      Message.error(res.msg)
    }
    this.loadChartLoading = false
  }
  @action
  getEnergyConservationData = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    let res = await getEnergyConservationData(params)
    if (res.data) {
      this.energyConservationData = res.data || []
    } else {
      Message.error(res.msg)
    }
  }
  @action
  getPueChart = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    this.pueLoading = true
    let res = await getPueChart(params)
    if (res.data) {
      this.pueChart = res.data || []
    } else {
      Message.error(res.msg)
    }
    this.pueLoading = false
  }
  @action
  getDayChart = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    let res = await getDayChart(params)
    if (res.data) {
      return res.data || {}
    } else {
      Message.error(res.msg)
    }
  }
  @action
  getRelayModuleInfo = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    let res = await getRelayModuleInfo(params)
    if (res.data) {
      this.deviceId = res.data.deviceId
      this.relayModuleInfo = res.data.relays || []
      this.relayModuleRecording = res.data.relays || []
    }
  }
  @action
  relayModuleControl = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    params["deviceId"] = toJS(this.deviceId)
    let res = await relayModuleControl(params)
    if (res.result === 0) {
      Message.success("请求成功")
      return res
    } else {
      Message.error(res.msg)
    }
  }
  @action
  relayModuleSet = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    params["deviceId"] = toJS(this.deviceId)
    let res = await relayModuleSet(params)
    if (res.result === 0) {
      Message.success(res.msg)
      return true
    } else {
      Message.error(res.msg)
    }
  }
  @action
  fsuControl = async (params = {}) => {
    let res = await fsuControl(params)
    if (res.result === 0) {
      Message.success("请求成功")
    } else {
      Message.error(res.msg)
    }
  }
  @action
  getFsuControlInfo = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    let res = await getFsuControlInfo(params)
    this.fsuControlInfo = res.data || {}
    this.fsuControRecording = res.data || {}
  }
  @action
  updateFsuControlInfo = async (params = {}, index) => {
    const fsuControlInfo = { ...this.fsuControlInfo }
    const voltages = fsuControlInfo.voltages || []

    const newInfo = voltages.map((item, i) => {
      if (index === i) {
        return params
      } else {
        return item
      }
    })
    this.fsuControlInfo = { ...fsuControlInfo, voltages: newInfo }
  }
  @action
  updateRelayModuleInfo = async (params = {}, index) => {
    const relayModuleInfo = [...this.relayModuleInfo]
    const newInfo = relayModuleInfo.map((item, i) => {
      if (index === i) {
        return params
      } else {
        return item
      }
    })
    this.relayModuleInfo = newInfo
  }

  @action
  getBattery = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    let res = await battery(params)
    this.battery = res.data || {}
    return res.data || {}
  }

  @action
  getElectricity = async (params = {}, forcedUpdate) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    let res = await electricity(params)
    this.electricity = res.data || {}
    if (res.data && res.data.electricitys && res.data.electricitys[0]) {
      if (forcedUpdate || !this.electricityLoadding) {
        let item = res.data.electricitys[this.chartCount]
        this.getChart({
          spId: item.spId,
          deviceId: item.deviceId,
        })
      }
    }
  }
  @action
  getChart = async (params = {}) => {
    const { chartCount, ...rest } = params
    this.chartCount =
      typeof chartCount === "number" ? chartCount : toJS(this.chartCount)
    this.electricityLoadding = true
    let res = await chart({
      ...rest,
      suId: localStorage.getItem("currentSuId") || "",
    })
    this.electricityLoadding = false
    this.chart = res.data || {}
  }
  @action
  getRectifierModule = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    let res = await rectifierModule(params)
    this.rectifierModule = res.data || {}
  }
  @action
  getRing = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    let res = await sealHead(params)
    this.ring = res.data || {}
  }
  @action
  getEnvironment = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""

    let res = await environment(params)
    this.environment = res.data || {}
  }
  @action
  getAlternatingCurrent = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    let res = await alternatingCurrent(params)
    this.alternatingCurrent = res.data || {}
  }

  @action
  getFsuInfo = async (params = {}) => {
    !params["suId"] &&
      (params["suId"] = localStorage.getItem("currentSuId") || "")
    let res = await getFsuInfo(params)
    this.fsuInfo = res.data || {}
    this.address = (res.data && res.data.address) || ""
    this.message = (res.data && res.data.message) || ""
  }

  @action
  getAlarm = async (params = {}) => {
    params["suId"] = localStorage.getItem("currentSuId") || ""
    let res = await alarm(params)
    this.alarm = res.data || {}
  }

  @action
  setDayChart = async (value) => {
    this.dayChart = value
  }
  @action
  confim = async (type) => {
    let obj = toJS(this.visible)
    obj[type] = true
    this.visible = obj
    this.currentType = type
  }
}
